/*
 *所有关于tb_project_workflowstep类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*tb_project_workflowstepService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using MYData.Core.BaseProvider;
using MYData.Core.Extensions.AutofacManager;
using MYData.Entity.DomainModels;
using System.Linq;
using MYData.Core.Utilities;
using System.Linq.Expressions;
using MYData.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MYData.System.IRepositories;
using MYData.Core.EFDbContext;
using MYData.Core.ManageUser;
using MYData.Entity.DomainModels.ApiEntity.Input;
using System.Collections.Generic;
using System;
using MYData.Core.Enums;
using MYData.Order.Services;
using Microsoft.AspNetCore.Components.Forms;
using System.Threading.Tasks;
using MYData.Core.Utilities.Response;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using Newtonsoft.Json;
using System.Dynamic;
using NUnit.Framework;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using MYData.Core.Dapper;
using System.Data;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System.Data.SqlClient;
using MYData.Core.DBManager;
using Microsoft.AspNetCore.Routing.Matching;
using Quartz.Util;
using Microsoft.AspNetCore.Components;
using System.Collections;

namespace MYData.System.Services
{
    public partial class tb_project_workflowstepService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Itb_project_workflowstepRepository _repository;//访问数据库
        private readonly ISqlDapper _dapperContext;
        private readonly Itb_project_workflowtableRepository _tableRepository;
        public  VOLContext _db;
        [ActivatorUtilitiesConstructor]
        public tb_project_workflowstepService(
            Itb_project_workflowstepRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            Itb_project_workflowtableRepository tableRepository,
            VOLContext db
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _tableRepository = tableRepository;
            _db = db;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        WebResponseContent webResponse = new WebResponseContent();
        UserInfo userInfo = UserContext.Current.UserInfo;
        /// <summary>
        /// 获取结点信息  用于当前流程的进度的展示
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> GetWorkFlowStep(string projectId)
        {
            
            var workFlowTable = _db.Set<tb_project_workflowtable>().Where(s => s.projectTableKey == projectId).FirstOrDefault();
            Sys_WorkFlow workFlow = _db.Set<Sys_WorkFlow>().Where(s => s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            var workFlowStep = _db.Set<Sys_WorkFlowStep>().Where(s => s.WorkFlow_Id == workFlowTable.workFlowId).Select(s=>new {s.StepName,s.StepId}).ToList();
            return webResponse.OK("success", workFlowStep);
        }
        /// <summary>
        /// 获取可退回结点信息
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> GetReturnWorkFlowStep(string projectId,string stepId)
        {

            var workFlowTable = _db.Set<tb_project_workflowtable>().Where(s => s.projectTableKey == projectId).FirstOrDefault();
            Sys_WorkFlow workFlow = _db.Set<Sys_WorkFlow>().Where(s => s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            int? orderId = _db.Set<Sys_WorkFlowStep>().Where(s => s.WorkFlow_Id == workFlowTable.workFlowId && s.StepId == stepId).FirstOrDefault().OrderId;
            var workFlowStep = _db.Set<Sys_WorkFlowStep>().Where(s => s.WorkFlow_Id == workFlowTable.workFlowId && s.OrderId < orderId).Select(s => new { s.StepName, s.OrderId }).ToList();
            return webResponse.OK("success", workFlowStep);
        }
        /// <summary>
        /// 获取当前审核详情  用于显示当前项目的全部审核
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> GetWorkStepInfo(string projectId)
        {
            var workFlowTable = _db.Set<tb_project_workflowtable>().Where(s => s.projectTableKey == projectId).FirstOrDefault();
            var workStepInfo = _db.Set<tb_project_workflowstep>().Where(s => s.workFlowTableId == workFlowTable.id);
            return webResponse.OK("success", workStepInfo);
        }
        /// <summary>
        /// 节点审核添加
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public WebResponseContent WorkFlowStepAddTs(WorkStepInput_Dto inputDto)
        {
            //获取指定流程项目关系表
            var workFlowTable = _db.Set<tb_project_workflowtable>().Where(s => s.projectTableKey == inputDto.projectId).FirstOrDefault();
            //获取流程表
            Sys_WorkFlow workFlow = _db.Set<Sys_WorkFlow>().Where(s => s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            List<dynamic> nodes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<dynamic>>(workFlow.NodeConfig);
            List<dynamic> lines = Newtonsoft.Json.JsonConvert.DeserializeObject<List<dynamic>>(workFlow.LineConfig);
            //获取流程项目关系表当前的流程节点
            List<Sys_WorkFlowStep> steps = _db.Set<Sys_WorkFlowStep>().Where(s => s.WorkFlow_Id == workFlow.WorkFlow_Id).ToList();
            Sys_WorkFlowStep workFlowStep = steps.Where(s => s.StepId == inputDto.auditStepId).FirstOrDefault();
            //为流程审核信息赋值
            tb_project_workflowstep tbStep = new tb_project_workflowstep();
            tbStep.Id = Ids.NextIdString();
            tbStep.workFlowTableId = workFlowTable.id;
            tbStep.workFlowId = workFlow.WorkFlow_Id.ToString();
            tbStep.stepId = workFlowStep.StepId;
            tbStep.stepName = workFlowStep.StepName;
            tbStep.stepType = workFlowStep.StepType;
            tbStep.stepValue = workFlowStep.StepValue;
            tbStep.orderId = workFlowStep.OrderId;
            tbStep.auditId = userInfo.User_Id;
            tbStep.auditor = userInfo.UserTrueName;
            tbStep.auditDate = DateTime.Now;
            tbStep.remark = inputDto.remark;
            //审核结果判断
            if (inputDto.Outcome)
            {
                //流程审核信息的审核状态
                tbStep.auditStatus = 1;
                //当前执行到的结点
                List<string> currentOrderIds = workFlowTable.currentOrderId.Split(',').ToList();
                currentOrderIds = currentOrderIds.Where(s => s != workFlowStep.OrderId.ToString()).ToList();
                //判断节点的判断
                List<dynamic> stepLine = lines.Where(s => s.from == inputDto.auditStepId).ToList();
                if (stepLine.Count() <= 0)
                {
                    workFlowTable.auditStatus = 1;
                }
                //知道当前判断节点的下一个结点
                List<dynamic> judgments = nodes.Where(s => stepLine.Select(t => t.to).Where(x => x == s.id).Count() > 0).ToList();
                string stepOrderId = "";
                foreach (var item in judgments)
                {
                    if (item.stepType == "false")
                    {
                        string id = item.id;
                        string current = JudgmentStep(steps, nodes, lines, workFlow, id, inputDto.projectId);
                        if (current == "danger")
                        {
                            return webResponse.Error("判断数据存在风险");
                        }

                        currentOrderIds.Add(current);
                    }
                    else
                    {
                        //判断节点的判断
                        List<dynamic> itemLine = lines.Where(s => s.to == item.id).Select(s => s.from).ToList();
                        List<string> itemOrders = steps.Where(a => itemLine.Exists(t => a.StepId.Contains(t.ToString()))).Select(s => s.OrderId.ToString()).ToList();
                        var exp2 = itemOrders.Where(a => !currentOrderIds.Exists(t => a.Contains(t))).ToList();
                        if (exp2.Count() == itemOrders.Count())
                        {
                            stepOrderId = item.id;
                            currentOrderIds.Add(_db.Set<Sys_WorkFlowStep>().Where(s => s.StepId == stepOrderId).Select(z => z.OrderId).FirstOrDefault().ToString());
                        }

                    }
                }


                string[] currentOrderId = currentOrderIds.ToArray();
                //数组去重
                currentOrderId = currentOrderId.Distinct().ToArray();
                //关系表的当前流程节点的跳转
                workFlowTable.currentOrderId = String.Join(",", currentOrderId);
                //var step = _db.Set<Sys_WorkFlowStep>().Where(s => s.OrderId == workFlowTable.currentOrderId && s.WorkFlow_Id == workFlowTable.workFlowId);
                //判断流程是否进行结束
                //if(step.Count()<1)
                //{
                //关系表的审核状态修改

                //}

            }
            else
            {
                tbStep.auditStatus = 2;
                if (string.IsNullOrEmpty(inputDto.orderId))
                {
                    workFlowTable.auditStatus = 2;
                }
                else
                {
                    workFlowTable.currentOrderId = inputDto.orderId;
                }
            }
            //基础信息添加
            _repository.Add(tbStep, true);
            //流程关系表修改
            _tableRepository.Update(workFlowTable, true);


            // 返回响应
            return webResponse.OK("success");
        }
        /// <summary>
        /// 获取判断节点后的结点id
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="lines"></param>
        /// <param name="workFlow">流程表</param>
        /// <param name="stepId">结点id</param>
        /// <param name="keyId">信息表id</param>
        /// <returns></returns>
        public string JudgmentStep(List<Sys_WorkFlowStep> steps, List<dynamic> nodes, List<dynamic> lines,Sys_WorkFlow workFlow, string stepId, string keyId)
        {
            string judStep = "true";
            judStep = JudgmentResult(workFlow, stepId, keyId);
            dynamic stepLine = null;
            string stepOrderId = "";
            if (judStep=="true")
            {
                stepLine = lines.Where(s => s.from == stepId && s.label==1).FirstOrDefault();
            }
            else if (judStep == "false")
            {
                stepLine = lines.Where(s => s.from == stepId && s.label == 2).FirstOrDefault();
            }
            else if (judStep == "danger")
            {
                return "danger";
            }
            dynamic judgments = nodes.Where(s => s.id == stepLine.to).FirstOrDefault();
            
            if (judgments.stepType == "false")
            {
                return JudgmentStep(steps,nodes, lines, workFlow, judgments.id, keyId);
            }
            else
            {
                stepOrderId = judgments.id;
                List<Sys_WorkFlowStep> step = steps.Where(s => s.StepId == stepOrderId).ToList();
                return step.Select(z => z.OrderId).FirstOrDefault().ToString();
            }
        }
        /// <summary>
        /// 结点判断
        /// </summary>
        /// <param name="workFlow"></param>
        /// <param name="stepId"></param>
        /// <param name="keyId"></param>
        /// <returns></returns>
        public string JudgmentResult(Sys_WorkFlow workFlow,string stepId,string keyId)
        {
            //反射指定数据库表类型
            Type t = typen(workFlow.WorkTable);
            //判断结果
            string result = "true";
            //获取自定义经费表的字段
            List<string> items = new List<string>();
            //var ss = t.Assembly.CreateInstance(t.Name);
            //实例化
            //var sss = Activator.CreateInstance(t);
            //MethodInfo mi = _db.GetType().GetMethod("Set").MakeGenericMethod(new Type[] {t});
            //var dbs = mi.Invoke(this, null);
            string sql = "";
            //序列化流程表结点信息
            List<dynamic> nodes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<dynamic>>(workFlow.NodeConfig);
            dynamic judgments = nodes.Where(s=>s.id == stepId).FirstOrDefault();
            var judgment = judgments.judgment;
            List<string> judgmentProperty = new List<string>();
            //判断当前类型是否存在
            if (t == null)
            {
                //经费报销信息表信息
                if (SqlFilter2(keyId))
                    return "false";
                sql = $"select * from tb_project_expensebaseinfo where id = {keyId}";
                //获取经费报销信息表类型
                Type type = typeof(tb_project_expensebaseinfo);
                //获取经费报销信息表字段
                PropertyInfo[] prop = type.GetProperties();
                foreach (var item in prop)
                {
                    //var name = item.GetCustomAttribute<DisplayNameAttribute>().DisplayName;
                    //var pName = t.GetProperty(item.Name.ToString());
                    //var displayName = pName.GetCustomAttribute<DisplayNameAttribute>();
                    //var name = displayName.DisplayName;
                    //加入 items
                    items.Add(item.Name);
                }
                
                //遍历判断条件
                foreach (var item in judgments.judgment)
                {
                    //判断条件字段是否存在实体表中
                    if(items.Contains(item.conditionInfo.ToString()))
                    {
                        if (SqlFilter2(item.between.ToString()) || SqlFilter2(item.conditionInfo.ToString()) || SqlFilter2(item.operators.ToString()) || SqlFilter2(item.conditionValue.ToString()))
                            return "danger";
                        //追加sql
                        sql = sql + " " + item.between + " " + item.conditionInfo + " " + item.operators + " " + " '" + item.conditionValue + " '";
                        //将追加的数据移除
                        judgment.Remove(item);
                    }
                }
                foreach (var item in judgment)
                {
                    judgmentProperty.Add(item.conditionInfo.ToString());
                }

            }
            else
            {
                //获取主键
                var s = t.GetKeyProperty();
                if (SqlFilter2(keyId))
                    return "danger";
                //表结构存在
                sql = $"select * from {workFlow.WorkTable} where {s.Name} = {keyId}";
                //循环追加sql
                foreach (var item in judgments.judgment)
                {
                    if (SqlFilter2(item.between) || SqlFilter2(item.conditionInfo) || SqlFilter2(item.operators) || SqlFilter2(item.conditionValue.ToString()))
                        return "danger";
                    sql = sql+ " " + item.between + " " + item.conditionInfo + " " + item.operators + " " + " '" + item.conditionValue + "'";
                }
            }
            if (_db.GetList(sql) == null)
            {
                return "false";
            }
            //执行sql
            var workTable = _db.GetList(sql).FirstOrDefault();
            
            //表结构是自定义并且判断条件未使用完
            if (t == null && judgmentProperty.Count()>0)
            {
                string json = "["+ workTable.subjectCostingInfo + "]";
                var info = JsonConvert.DeserializeObject<dynamic>(json);
                //PropertyInfo[] props = info.GetType().GetProperties();
                //foreach (var item in props)
                //{

                //}
                
                int ands = 0 ;
                int andsTrue = 0;
                foreach (var item in judgment)
                {
                    if (judgmentProperty.Contains(item.conditionInfo.ToString()))
                    {
                        var property = info[0][item.conditionInfo.ToString()];
                        string propertys = property.ToString();
                        if(item.between=="and")
                        {
                            ands++;
                        switch (item.operators.ToString())
                        {
                            case "=":
                                result = property == item.conditionValue?"true":"false";
                                    if (result=="true")
                                        andsTrue++;
                                    ; break;
                            case "!=":
                                result = property != item.conditionValue ? "true" : "false";
                                    if (result == "true")
                                        andsTrue++;
                                    ; break;
                            case ">":
                                result = property > item.conditionValue ? "true" : "false";
                                    if (result == "true")
                                        andsTrue++;
                                    ; break;
                            case ">=":
                                result = property >= item.conditionValue ? "true" : "false";
                                    if (result == "true")
                                        andsTrue++;
                                    ; break;
                            case "<":
                                result = property < item.conditionValue ? "true" : "false";
                                    if (result == "true")
                                        andsTrue++;
                                    ; break;
                            case "<=":
                                result = property <= item.conditionValue ? "true" : "false";
                                    if (result == "true")
                                        andsTrue++;
                                    ; break;
                            case "like":
                                result = propertys.IndexOf(item.conditionValue.ToString())>=0 ? "true" : "false";
                                    if (result == "true")
                                        andsTrue++;
                                    ; break;
                            case "not like":
                                result = propertys.IndexOf(item.conditionValue.ToString()) <0 ? "true" : "false";
                                    if (result == "true")
                                        andsTrue++;
                                    ; break;
                        }
                        }
                        else if(item.between == "or")
                        {
                            switch (item.operators.ToString())
                            {
                                case "=":
                                    result = property == item.conditionValue?"true" : "false";
                                    if (result == "true")
                                        return result;
                                    ; break;
                                case "!=":
                                    result = property != item.conditionValue ? "true" : "false";
                                    if (result == "true")
                                        return result;
                                    ; break;
                                case ">":
                                    result = property > item.conditionValue ? "true" : "false";
                                    if (result == "true")
                                        return result;
                                    ; break;
                                case ">=":
                                    result = property >= item.conditionValue ? "true" : "false";
                                    if (result == "true")
                                        return result;
                                    ; break;
                                case "<":
                                    result = property < item.conditionValue ? "true" : "false";
                                    if (result == "true")
                                        return result;
                                    ; break;
                                case "<=":
                                    result = property <= item.conditionValue ? "true" : "false";
                                    if (result == "true")
                                        return result;
                                    ; break;
                                case "like":
                                    result = propertys.IndexOf(item.conditionValue.ToString()) >= 0 ? "true" : "false";
                                    if (result == "true")
                                        return result;
                                    ; break;
                                case "not like":
                                    result = propertys.IndexOf(item.conditionValue.ToString()) < 0 ? "true" : "false";
                                    if (result == "true")
                                        return result;
                                    ; break;
                            }
                        }
                    }
                }
                if (ands == andsTrue)
                    result = "true";
            }
            
            return result;
        }
        /// <summary>
        /// 获取进度
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public string GetProgress(string projectId, string tableData)
        {
            //映射类名称
            Type type = typen(tableData);
            string tableName = "";
            //判断是否自定义表
            if (type == null)
            {
                tableName = tableData;
            }
            else
            {
                tableName = type.Name;
            }
            //流程表
            var workFlow = _db.Set<Sys_WorkFlow>().Where(s => s.WorkTable == tableName).FirstOrDefault();
            //流程项目关系表
            var pro = _db.Set<tb_project_workflowtable>().Where(s => s.projectTableKey == projectId).FirstOrDefault();
            //流程审批节点表
            List<string> step = new List<string>();
            if (pro == null)
                return "";
            step = _db.Set<Sys_WorkFlowStep>().Where(s => s.WorkFlow_Id == workFlow.WorkFlow_Id && pro.currentOrderId.Contains(s.OrderId.ToString())).Select(s=>s.StepName).ToList();
            if (pro.auditStatus == 0)
            {
                
                string s = String.Join(",", step);
                return s;
            }
            if (pro.auditStatus == 1)
            {
                return "审核通过";
            }
            if (pro.auditStatus == 2)
            {
                return "审核未通过";
            }
            return "";
        }
        /// <summary>
        /// 获取操作
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public  List<TypeName> GetOperate(string projectId,string tableData)
        {
            //映射类名称
            Type type = typen(tableData);
            string tableName = "";
            //判断是否自定义表
            if (type == null)
            {
                tableName = tableData;
            }
            else
            {
                tableName = type.Name;
            }
            tb_project_baseinfo baseInfo = _db.Set<tb_project_baseinfo>().Where(s => s.projectId == projectId).FirstOrDefault();
            Sys_WorkFlow workFlow = _db.Set<Sys_WorkFlow>().Where(s => s.WorkTable == tableName).FirstOrDefault();
            var pro = _db.Set<tb_project_workflowtable>().Where(s => s.projectTableKey == projectId).FirstOrDefault();
            if (pro == null  || pro.auditStatus != 0)
                return null;
            //获取当前的结点
            List<Sys_WorkFlowStep> step = _db.Set<Sys_WorkFlowStep>().Where(s => s.WorkFlow_Id == workFlow.WorkFlow_Id && pro.currentOrderId.Contains(s.OrderId.ToString())).ToList();

            ////负责人返回操作
            //if (pro.currentOrderId == 2 && baseInfo.projectPerson == userInfo.UserName)
            //{
            //    return step.StepName;
            //}
            //根据角色操作
            List<TypeName> Operates = new List<TypeName>();
            foreach (var item in step)
            {
                if (item.StepType ==2 && userInfo.Role_Id == item.StepValue)
                {
                    Operates.Add(new TypeName{
                       Name = item.StepName,
                       Field = item.StepId
                    });
                }
                //根据指定用户操作
                if (item.StepType == 1 && userInfo.User_Id == item.StepValue)
                {
                    Operates.Add(new TypeName
                    {
                        Name = item.StepName,
                        Field = item.StepId
                    });
                }
                //根据部门操作
                if (item.StepType == 3 && userInfo.DeptId == item.StepValue)
                {
                    Operates.Add(new TypeName
                    {
                        Name = item.StepName,
                        Field = item.StepId
                    });
                }
            }
            return Operates;
        }

        /// <summary>
        /// 数据字符串获取实体
        /// </summary>
        /// <param name="typeName">实体的名称</param>
        /// <returns></returns>
        public static Type typen(string typeName)
        {
            Type type = null;
            Assembly[] assemblyArray = AppDomain.CurrentDomain.GetAssemblies();
            int assemblyArrayLength = assemblyArray.Length;
            for (int i = 0; i < assemblyArrayLength; ++i)
            {
                type = assemblyArray[i].GetType(typeName);
                if (type != null)
                {
                    return type;
                }
            }

            for (int i = 0; (i < assemblyArrayLength); ++i)
            {
                Type[] typeArray = assemblyArray[i].GetTypes();
                int typeArrayLength = typeArray.Length;
                for (int j = 0; j < typeArrayLength; ++j)
                {
                    if (typeArray[j].Name.Equals(typeName))
                    {
                        return typeArray[j];
                    }
                }
            }
            return type;
        }
        /// <summary>
        /// 根据表名获取查询字段的集合
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public WebResponseContent JudgmentConditions(string tableName)
        {
            if (string.IsNullOrEmpty(tableName))
            return webResponse.OK("获取信息成功", null);
            List<TypeName> list = new List<TypeName>();
            Type t = typen(tableName);
            
            string json = "";
            if (t== null)
            {
                t = typen("tb_project_expensebaseinfo");
                json  = _db.Set<tb_project_subjectexpensedetailinfo>().Where(s=>s.subjectId == tableName).FirstOrDefault().jsonIsJudge;
            }
            PropertyInfo[] props = t.GetProperties();
            foreach (var item in props)
            {
                //var name = item.GetCustomAttribute<DisplayNameAttribute>().DisplayName;
                //var pName = t.GetProperty(item.Name.ToString());
                //var displayName = pName.GetCustomAttribute<DisplayNameAttribute>();
                //var name = displayName.DisplayName;
                TypeName typeName = new TypeName();
                typeName.Name = (TypeDescriptor.GetProperties(t)[item.Name.ToString()].Attributes[typeof(DisplayAttribute)] as DisplayAttribute).Name;
                typeName.Field = item.Name;
                list.Add(typeName);
            }
            if (json != "")
            {
                List<dynamic> DynamicObject = JsonConvert.DeserializeObject<List<dynamic>>(json);
                foreach (var item in DynamicObject)
                {
                    TypeName typeName = new TypeName();
                    typeName.Name = item.label;
                    if(item.label=="")
                    {
                        typeName.Name = item.id;
                    }
                    typeName.Field = item.id;
                    list.Add(typeName);
                }
            }
                return webResponse.OK("获取信息成功", list);
        }
        /// <summary>
        /// 新增入账单节点审核添加
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public WebResponseContent newAccountAdd(NewAccountInput_Dto inputDto)
        {
            //获取指定流程项目关系表
            var workFlowTable = _db.Set<tb_project_workflowtable>().Where(s => s.projectTableKey == inputDto.billingId).FirstOrDefault();
            //获取流程表
            Sys_WorkFlow workFlow = _db.Set<Sys_WorkFlow>().Where(s => s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            //获取流程项目关系表当前的流程节点
            Sys_WorkFlowStep workFlowStep = _db.Set<Sys_WorkFlowStep>().Where(s => s.OrderId.ToString() == workFlowTable.currentOrderId && s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            //为流程审核信息赋值
            tb_project_workflowstep tbStep = new tb_project_workflowstep();
            tbStep.workFlowTableId = workFlowTable.id;
            tbStep.workFlowId = workFlow.WorkFlow_Id.ToString();
            tbStep.stepId = workFlowStep.StepId;
            tbStep.stepName = workFlowStep.StepName;
            tbStep.stepType = workFlowStep.StepType;
            tbStep.stepValue = workFlowStep.StepValue;
            tbStep.orderId = workFlowStep.OrderId;
            tbStep.auditId = userInfo.User_Id;
            tbStep.auditor = userInfo.UserTrueName;
            tbStep.auditDate = DateTime.Now;
            tbStep.remark = inputDto.remark;
            //审核结果判断
            if (inputDto.Outcome)
            {
                //流程审核信息的审核状态
                tbStep.auditStatus = 1;
                //关系表的当前流程节点的跳转
                workFlowTable.currentOrderId += 1;
                var step = _db.Set<Sys_WorkFlowStep>().Where(s => s.OrderId.ToString() == workFlowTable.currentOrderId && s.WorkFlow_Id == workFlowTable.workFlowId);
                //判断流程是否进行结束
                if (step.Count() < 1)
                {
                    //关系表的审核状态修改
                    workFlowTable.auditStatus = 1;
                }


            }
            else
            {
                tbStep.auditStatus = 2;
                if (!string.IsNullOrEmpty(inputDto.orderId))
                {
                    workFlowTable.auditStatus = 2;
                }
                else
                {
                    workFlowTable.currentOrderId = inputDto.orderId;
                }
            }


            //基础信息添加
            _repository.Add(tbStep, true);
            //流程关系表修改
            _tableRepository.Update(workFlowTable, true);

            // 返回响应
            return webResponse.OK("success");
        }
        /// <summary>
        /// 经费分配节点审核添加
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public WebResponseContent newAllotAdd(NewAllotInput_Dto inputDto)
        {
            //获取指定流程项目关系表
            var workFlowTable = _db.Set<tb_project_workflowtable>().Where(s => s.projectTableKey == inputDto.allocationId).FirstOrDefault();
            //获取流程表
            Sys_WorkFlow workFlow = _db.Set<Sys_WorkFlow>().Where(s => s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            //获取流程项目关系表当前的流程节点
            Sys_WorkFlowStep workFlowStep = _db.Set<Sys_WorkFlowStep>().Where(s => s.OrderId.ToString() == workFlowTable.currentOrderId && s.WorkFlow_Id == workFlowTable.workFlowId).FirstOrDefault();
            //为流程审核信息赋值
            tb_project_workflowstep tbStep = new tb_project_workflowstep();
            tbStep.workFlowTableId = workFlowTable.id;
            tbStep.workFlowId = workFlow.WorkFlow_Id.ToString();
            tbStep.stepId = workFlowStep.StepId;
            tbStep.stepName = workFlowStep.StepName;
            tbStep.stepType = workFlowStep.StepType;
            tbStep.stepValue = workFlowStep.StepValue;
            tbStep.orderId = workFlowStep.OrderId;
            tbStep.auditId = userInfo.User_Id;
            tbStep.auditor = userInfo.UserTrueName;
            tbStep.auditDate = DateTime.Now;
            tbStep.remark = inputDto.remark;
            //审核结果判断
            if (inputDto.Outcome)
            {
                //流程审核信息的审核状态
                tbStep.auditStatus = 1;
                //关系表的当前流程节点的跳转
                workFlowTable.currentOrderId += 1;
                var step = _db.Set<Sys_WorkFlowStep>().Where(s => s.OrderId.ToString() == workFlowTable.currentOrderId && s.WorkFlow_Id == workFlowTable.workFlowId);
                //判断流程是否进行结束
                if (step.Count() < 1)
                {
                    //关系表的审核状态修改
                    workFlowTable.auditStatus = 1;
                }

            }
            else
            {
                tbStep.auditStatus = 2;
                if (!string.IsNullOrEmpty(inputDto.orderId))
                {
                    workFlowTable.auditStatus = 2;
                }
                else
                {
                    workFlowTable.currentOrderId = inputDto.orderId;
                }
            }

            //基础信息添加
            _repository.Add(tbStep, true);
            //流程关系表修改
            _tableRepository.Update(workFlowTable, true);


            // 返回响应
            return webResponse.OK("success");
        }

        //防sql注入字符串过滤
        public static bool SqlFilter2(string InText)
        {
           string word = "'|and|exec|insert|select|delete|update|count|drop table|*|%|chr|mid|master|truncate|char|declare|;|or|-|+|,";
            if (InText == null )
             return  false;
           foreach(string i  in  word.Split("|"))
           {
             if((InText.ToLower().IndexOf(i) >- 1 ))
            {
              return  true;
            }
           }
            return  false;
         }
    }
}
